﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class ProductFormViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Private variables
        private Product _product;
        private List<Category> _categories;
        private List<Supplier> _suppliers;
        private bool _firstOpen = true;
        #endregion

        #region Constructor
        public ProductFormViewModel()
        {
        }

        public ProductFormViewModel(List<Category> c, List<Supplier> s)
        {
            this._categories = c;
            this._suppliers = s;
            this._product = Product.CreateProduct();
        }

        public ProductFormViewModel(ActionModes mode, Product p, List<Category> c, List<Supplier> s)
        {
            this.FormMode = mode;
            this._categories = c;
            this._suppliers = s;
            this._product = p;

            this.LoadDataFromCurrentItem();
        }

        private void LoadDataFromCurrentItem()
        {
            foreach (Supplier s in this._suppliers)
            {
                if (s.SupplierUNK.Equals(this._product.SupplierUNK))
                {
                    this.SelectedSupplier = s;
                    OnPropertyChanged("SelectedSupplier");
                    break;
                }
            }

            foreach (Category c in this._categories)
            {
                if (c.CategoryUNK.Equals(this._product.CategoryUNK))
                {
                    this.SelectedCategory = c;
                    OnPropertyChanged("SelectedCategory");
                    break;
                }
            }
        }

        public Product CurrentProduct
        {
            get
            {
                return this._product;
            }
        }

        public ActionModes FormMode
        {
            get;
            set;
        }
        #endregion

        #region Product Properties
        
        public string Description
        {
            get { return _product.Description; }
            set
            {
                if (value == _product.Description)
                    return;

                _product.Description = value;

                base.OnPropertyChanged("Description");
            }
        }

        public string Name
        {
            get { return _product.Name; }
            set
            {
                if (value == _product.Name)
                    return;

                _product.Name = value;

                base.OnPropertyChanged("Name");
            }
        }

        public double WholesalePrice
        {
            get { return _product.WholesalePrice; }
            set
            {
                if (value == _product.WholesalePrice)
                    return;

                _product.WholesalePrice = value;

                base.OnPropertyChanged("WholesalePrice");
            }
        }

        public double RetailPrice
        {
            get { return _product.RetailPrice; }
            set
            {
                if (value == _product.RetailPrice)
                    return;

                _product.RetailPrice = value;

                base.OnPropertyChanged("RetailPrice");
            }
        }

        public double ImportPrice
        {
            get { return _product.ImportPrice; }
            set
            {
                if (value == _product.ImportPrice)
                    return;

                _product.ImportPrice = value;

                base.OnPropertyChanged("ImportPrice");
            }
        }

        public string SerialNo
        {
            get { return _product.SerialNo; }
            set
            {
                if (value == _product.SerialNo)
                    return;

                _product.SerialNo = value;

                base.OnPropertyChanged("SerialNo");
            }
        }

        public List<Category> Categories
        {
            get
            {
                return this._categories;
            }
        }

        public List<Supplier> Suppliers
        {
            get
            {
                return this._suppliers;
            }
        }

        public Category SelectedCategory
        {
            get;
            set;
        }

        public Supplier SelectedSupplier
        {
            get;
            set;
        }

        #endregion // Product Properties

        #region Commands
        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(),
                        (param) => this._product.IsValid
                        );
                }
                return _saveCommand;
            }
        }

        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(
                        (param) => this.Delete(), (param) => this.CanDelete);
                }
                return _deleteCommand;
            }
        }

        #endregion

        #region Private helpers
        /// <summary>
        /// Returns true if the customer is valid and can be saved.
        /// </summary>
        bool CanSave
        {
            get { return this._firstOpen ? true : _product.IsValid; }
        }

        /// <summary>
        /// Execute save function
        /// </summary>
        private void Save()
        {
            this._firstOpen = false;
            if (!this._product.IsValid)
            {
                base.OnPropertyChanged("Name");
                return;
            }

            if (string.IsNullOrEmpty(this._product.ProductUNK))
            {
                this._product.ProductUNK = "Pro-" + Guid.NewGuid().ToString();
            }

            this._product.CategoryUNK = this.SelectedCategory.CategoryUNK;
            this._product.CategoryName = this.SelectedCategory.Name;
            this._product.SupplierUNK = this.SelectedSupplier.SupplierUNK;

            commandExcute = "Save";
            OnRequestClose();
        }

        void Close()
        {
            commandExcute = "Close";
            OnRequestClose();
        }

        bool CanDelete
        {
            get { return string.IsNullOrEmpty(this._product.ProductUNK) ? false : true; }
        }

        void Delete()
        {
            commandExcute = "Delete";
            OnRequestClose();
        }
        #endregion

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error
        {
            get { return (_product as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                    error = (_product as IDataErrorInfo)[propertyName];


                    // Dirty the commands registered with CommandManager,
                    // such as our Save command, so that they are queried
                    // to see if they can execute now.
                    CommandManager.InvalidateRequerySuggested();

                return error;
            }
        }

        #endregion // IDataErrorInfo Members

        public string commandExcute { get; set; }

        
    }
}
